#
# Copyright 2021 The Modelbox Project Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cmake_minimum_required(VERSION 3.10)
set(CPACK_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "")
set(CPACK_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR} CACHE INTERNAL "")
function(MODELBOX_CPACK)
    set(CPACK_RPM_COMPONENT_INSTALL ON PARENT_SCOPE)
    set(CPACK_DEB_COMPONENT_INSTALL ON PARENT_SCOPE)
    set(CPACK_ARCHIVE_COMPONENT_INSTALL OFF PARENT_SCOPE)
    set(CPACK_SET_DESTDIR ON PARENT_SCOPE)
    set(CPACK_STRIP_FILES ON PARENT_SCOPE)
    find_program(TAR tar)
    find_program(DPKG dpkg)
    find_program(RPM rpm)
    set(MODELBOX_PACK_NAME "${CPACK_PACKAGE_NAME}")
    if(NOT CPACK_PACKAGE_NAME)
        set(MODELBOX_PACK_NAME "${CMAKE_PROJECT_NAME}")
    endif()
    
    if(TAR) 
        set(CPACK_GENERATOR "${CPACK_GENERATOR}TGZ;")
    endif()
    if (NOT STANDALONE)
        if(DPKG) 
            set(CPACK_GENERATOR "${CPACK_GENERATOR}DEB;")
        endif()
        if(RPM) 
            set(CPACK_GENERATOR "${CPACK_GENERATOR}RPM;")
        endif()
    endif()

    set(CPACK_GENERATOR "${CPACK_GENERATOR}" PARENT_SCOPE)

    set(CPACK_COMPONENT_CPU-DEVICE-FLOWUNIT_DESCRIPTION "Modelbox CPU flowunit plugins" PARENT_SCOPE)
    set(CPACK_COMPONENT_CPU-DEVICE-FLOWUNIT-DEVEL_DESCRIPTION "Modelbox CPU flowunit plugins - development" PARENT_SCOPE)
    set(CPACK_COMPONENT_ASCEND-DEVICE-FLOWUNIT_DESCRIPTION "Modelbox Ascend flowunit plugins" PARENT_SCOPE)
    set(CPACK_COMPONENT_ASCEND-DEVICE-FLOWUNIT-DEVEL_DESCRIPTION "Modelbox Ascend flowunit plugins - development" PARENT_SCOPE)
    set(CPACK_COMPONENT_CUDA-DEVICE-FLOWUNIT_DESCRIPTION "Modelbox Cuda flowunit plugins" PARENT_SCOPE)
    set(CPACK_COMPONENT_CUDA-DEVICE-FLOWUNIT-DEVEL_DESCRIPTION "Modelbox Cuda flowunit plugins - development" PARENT_SCOPE)
    set(CPACK_COMPONENT_GRAPH-GRAPHVIZ_DESCRIPTION "Modelbox graph parser for graphviz" PARENT_SCOPE)
    set(CPACK_COMPONENT_VIRTUALDRIVER-DRIVER-INFERENCE_DESCRIPTION "Modelbox virtual plugin for inference" PARENT_SCOPE)
    set(CPACK_COMPONENT_VIRTUALDRIVER-DRIVER-PYTHON_DESCRIPTION "Modelbox virtual plugin for python" PARENT_SCOPE)
    set(CPACK_COMPONENT_SOLUTION_DESCRIPTION "Modelbox solutions" PARENT_SCOPE)
    set(CPACK_COMPONENT_SERVER_DESCRIPTION "Modelbox service" PARENT_SCOPE)
    set(CPACK_COMPONENT_SERVER-DEVEL_DESCRIPTION "Modelbox service - development" PARENT_SCOPE)
    set(CPACK_COMPONENT_DOCUMENT_DESCRIPTION "Modelbox document" PARENT_SCOPE)
    set(CPACK_COMPONENT_LIBMODELBOX_DESCRIPTION "Modelbox framework library" PARENT_SCOPE)
    set(CPACK_COMPONENT_LIBMODELBOX-DEVEL_DESCRIPTION "Modelbox framework library - development" PARENT_SCOPE)   

    # deb package configuration
    set(CPACK_DEBIAN_ENABLE_COMPONENT_DEPENDS ON PARENT_SCOPE)
    set(CPACK_DEBIAN_CPU-DEVICE-FLOWUNIT_PACKAGE_SECTION "libs" PARENT_SCOPE)
    set(CPACK_DEBIAN_CPU-DEVICE-FLOWUNIT_PACKAGE_DEPENDS "${MODELBOX_PACK_NAME}-libmodelbox" PARENT_SCOPE)
    set(CPACK_DEBIAN_CPU-DEVICE-FLOWUNIT_PACKAGE_CONTROL_EXTRA ${CPACK_SOURCE_DIR}/debian/postinst PARENT_SCOPE)
    set(CPACK_DEBIAN_CUDA-DEVICE-FLOWUNIT_PACKAGE_SECTION "libs" PARENT_SCOPE)
    set(CPACK_DEBIAN_CUDA-DEVICE-FLOWUNIT_PACKAGE_DEPENDS "${MODELBOX_PACK_NAME}-libmodelbox" PARENT_SCOPE)
    set(CPACK_DEBIAN_CUDA-DEVICE-FLOWUNIT_PACKAGE_CONTROL_EXTRA ${CPACK_SOURCE_DIR}/debian/postinst PARENT_SCOPE)
    set(CPACK_DEBIAN_ASCEND-DEVICE-FLOWUNIT_PACKAGE_SECTION "libs" PARENT_SCOPE)
    set(CPACK_DEBIAN_ASCEND-DEVICE-FLOWUNIT_PACKAGE_DEPENDS "${MODELBOX_PACK_NAME}-libmodelbox" PARENT_SCOPE)
    set(CPACK_DEBIAN_ASCEND-DEVICE-FLOWUNIT_PACKAGE_CONTROL_EXTRA ${CPACK_SOURCE_DIR}/debian/postinst PARENT_SCOPE)
    set(CPACK_DEBIAN_GRAPH-GRAPHVIZE_PACKAGE_SECTION "libs" PARENT_SCOPE)
    set(CPACK_DEBIAN_GRAPH-GRAPHVIZE_PACKAGE_DEPENDS "${MODELBOX_PACK_NAME}-libmodelbox" PARENT_SCOPE)
    set(CPACK_DEBIAN_GRAPH-GRAPHVIZE_PACKAGE_CONTROL_EXTRA ${CPACK_SOURCE_DIR}/debian/postinst PARENT_SCOPE)
    set(CPACK_DEBIAN_VIRTUALDRIVER-DRIVER-INFERENCE_PACKAGE_SECTION "libs" PARENT_SCOPE)
    set(CPACK_DEBIAN_VIRTUALDRIVER-DRIVER-INFERENCE_PACKAGE_DEPENDS "${MODELBOX_PACK_NAME}-libmodelbox" PARENT_SCOPE)
    set(CPACK_DEBIAN_VIRTUALDRIVER-DRIVER_PACKAGE_CONTROL_EXTRA ${CPACK_SOURCE_DIR}/debian/postinst PARENT_SCOPE)
    set(CPACK_DEBIAN_VIRTUALDRIVER-DRIVER-PYTHON_PACKAGE_SECTION "libs" PARENT_SCOPE)
    set(CPACK_DEBIAN_VIRTUALDRIVER-DRIVER-PYTHON_PACKAGE_DEPENDS "${MODELBOX_PACK_NAME}-libmodelbox" PARENT_SCOPE)
    set(CPACK_DEBIAN_VIRTUALDRIVER-DRIVER-PYTHON_PACKAGE_CONTROL_EXTRA ${CPACK_SOURCE_DIR}/debian/postinst PARENT_SCOPE)
    set(CPACK_DEBIAN_DEMO_PACKAGE_SECTION "libs" PARENT_SCOPE)
    set(CPACK_DEBIAN_DEMO_PACKAGE_DEPENDS "${MODELBOX_PACK_NAME}-libmodelbox" PARENT_SCOPE)
    set(CPACK_DEBIAN_DEMO_PACKAGE_CONTROL_EXTRA ${CPACK_SOURCE_DIR}/debian/postinst PARENT_SCOPE)
    configure_file(${CPACK_SOURCE_DIR}/debian/modelbox-server/postinst.in ${CPACK_BINARY_DIR}/debian/modelbox-server/postinst @ONLY)
    configure_file(${CPACK_SOURCE_DIR}/debian/modelbox-server/postrm.in ${CPACK_BINARY_DIR}/debian/modelbox-server/postrm @ONLY)
    configure_file(${CPACK_SOURCE_DIR}/debian/modelbox-server/conffiles.in ${CPACK_BINARY_DIR}/debian/modelbox-server/conffiles @ONLY)
    set(CPACK_DEBIAN_SERVER_PACKAGE_SECTION "s" PARENT_SCOPE)
    set(CPACK_DEBIAN_SERVER_PACKAGE_DEPENDS "${MODELBOX_PACK_NAME}-libmodelbox" PARENT_SCOPE)
    set(CPACK_DEBIAN_SERVER_PACKAGE_CONTROL_EXTRA
        ${CPACK_BINARY_DIR}/debian/modelbox-server/postinst
        ${CPACK_BINARY_DIR}/debian/modelbox-server/postrm
        ${CPACK_BINARY_DIR}/debian/modelbox-server/postinst/conffiles PARENT_SCOPE)

    set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Huawei Technologies Co., Ltd." PARENT_SCOPE)

    # rpm package configuration
    set(CPACK_PACKAGE_RELOCATABLE OFF PARENT_SCOPE)
    set(CPACK_RPM_PACKAGE_AUTOREQ OFF PARENT_SCOPE)
    set(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION
        "/usr/local"
        "/usr/local/bin"
        "/usr/local/lib"
        "/usr/local/lib64"
        "/usr/local/etc"
        "/usr/local/include"
        "/usr/lib/systemd"
        ${CMAKE_INSTALL_FULL_BINDIR}
        ${CMAKE_INSTALL_FULL_INCLUDEDIR}
        ${CMAKE_INSTALL_FULL_LIBDIR}
        "${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig"
        ${SYSTEMDSYSTEMUNITDIR} PARENT_SCOPE)
    set(CPACK_RPM_CPU-DEVICE-FLOWUNIT_PACKAGE_REQUIRES "${MODELBOX_PACK_NAME}-libmodelbox" PARENT_SCOPE)
    set(CPACK_RPM_CPU-DEVICE-FLOWUNIT_POST_INSTALL_SCRIPT_FILE ${CPACK_SOURCE_DIR}/rpm/postscript PARENT_SCOPE)
    set(CPACK_RPM_CUDA-DEVICE-FLOWUNIT_PACKAGE_REQUIRES "${MODELBOX_PACK_NAME}-libmodelbox" PARENT_SCOPE)
    set(CPACK_RPM_CUDA-DEVICE-FLOWUNIT_POST_INSTALL_SCRIPT_FILE ${CPACK_SOURCE_DIR}/rpm/postscript PARENT_SCOPE)
    set(CPACK_RPM_ASCEND-DEVICE-FLOWUNIT_PACKAGE_REQUIRES "${MODELBOX_PACK_NAME}-libmodelbox" PARENT_SCOPE)
    set(CPACK_RPM_ASCEND-DEVICE-FLOWUNIT_POST_INSTALL_SCRIPT_FILE ${CPACK_SOURCE_DIR}/rpm/postscript PARENT_SCOPE)
    set(CPACK_RPM_GRAPH-GRAPHVIZE_PACKAGE_REQUIRES "${MODELBOX_PACK_NAME}-libmodelbox" PARENT_SCOPE)
    set(CPACK_RPM_GRAPH-GRAPHVIZE_POST_INSTALL_SCRIPT_FILE ${CPACK_SOURCE_DIR}/rpm/postscript PARENT_SCOPE)
    set(CPACK_RPM_VIRTUALDRIVER-DRIVER-INFERENCE_PACKAGE_REQUIRES "${MODELBOX_PACK_NAME}-libmodelbox" PARENT_SCOPE)
    set(CPACK_RPM_VIRTUALDRIVER-DRIVER-INFERENCE_POST_INSTALL_SCRIPT_FILE ${CPACK_SOURCE_DIR}/rpm/postscript PARENT_SCOPE)
    set(CPACK_RPM_VIRTUALDRIVER-DRIVER-PYTHON_PACKAGE_REQUIRES "${MODELBOX_PACK_NAME}-libmodelbox" PARENT_SCOPE)
    set(CPACK_RPM_VIRTUALDRIVER-DRIVER-PYTHON_POST_INSTALL_SCRIPT_FILE ${CPACK_SOURCE_DIR}/rpm/postscript PARENT_SCOPE)
    set(CPACK_RPM_DEMO_PACKAGE_REQUIRES "${MODELBOX_PACK_NAME}-libmodelbox" PARENT_SCOPE)
    set(CPACK_RPM_DEMO_POST_INSTALL_SCRIPT_FILE ${CPACK_SOURCE_DIR}/rpm/demo/postscript PARENT_SCOPE)
    set(CPACK_RPM_SERVER_PACKAGE_REQUIRES "${MODELBOX_PACK_NAME}-libmodelbox, ${MODELBOX_PACK_NAME}-graph-graphviz" PARENT_SCOPE)
    configure_file(${CPACK_SOURCE_DIR}/rpm/modelbox-server/postscript.in ${CPACK_BINARY_DIR}/rpm/modelbox-server/postscript @ONLY)
    configure_file(${CPACK_SOURCE_DIR}/rpm/modelbox-server/postunscript.in ${CPACK_BINARY_DIR}/rpm/modelbox-server/postunscript @ONLY)
    configure_file(${CPACK_SOURCE_DIR}/rpm/modelbox-server/conffiles.in ${CPACK_BINARY_DIR}/rpm/modelbox-server/conffiles @ONLY)
    set(CPACK_RPM_SERVER_POST_INSTALL_SCRIPT_FILE ${CPACK_BINARY_DIR}/rpm/modelbox-server/postscript PARENT_SCOPE)
    set(CPACK_RPM_SERVER_POST_UNINSTALL_SCRIPT_FILE ${CPACK_BINARY_DIR}/rpm/modelbox-server/postunscript PARENT_SCOPE)
    set(CPACK_RPM_SERVER_USER_FILELIST 
        "%config ${CMAKE_INSTALL_FULL_SYSCONFDIR}/modelbox/modelbox.conf"
        "%config ${CMAKE_INSTALL_FULL_SYSCONFDIR}/modelbox/modelbox-opts" PARENT_SCOPE)
    set(CPACK_OUTPUT_FILE_PREFIX ${RELEASE_PACKAGE_DIR} PARENT_SCOPE)
    set(CPACK_PACKAGE_DIRECTORY ${CMAKE_BINARY_DIR}/cpack PARENT_SCOPE)
    
    get_cmake_property(CPACK_COMPONENTS_ALL COMPONENTS)
    list(REMOVE_ITEM CPACK_COMPONENTS_ALL "Unspecified")
    set(CPACK_COMPONENTS_ALL ${CPACK_COMPONENTS_ALL} PARENT_SCOPE) 
endfunction(MODELBOX_CPACK)

set(${MODELBOX_CPACK} ${MODELBOX_CPACK} CACHE INTERNAL "")
set(CPACK_PACKAGE_VERSION_MAJOR ${MODELBOX_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${MODELBOX_VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${MODELBOX_VERSION_PATCH})

if (NOT DISABLE_MODELBOX_CPACK)
    include(CPackComponent)
    MODELBOX_CPACK()
    include(CPack)
endif()